import tensorflow as tf
tf.__version__
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import warnings
warnings.filterwarnings('ignore')
pic = Image.open('c:/Users/Hari/Desktop/datasets/Naresh_datasets/00-puppy.jpg')
pic
# what type of object
print(type(pic))
# converting the image into array
pic_arr = np.asarray(pic)
type(pic_arr)
pic_arr # here number of dimension represet number of square brackst
color image has three dimension i.e 3rd dimenion is color channel that is RGB
blackwhite image has only two dimension i.e intensity represent the black color
pic_arr.shape
#displaying the image
plt.imshow(pic_arr)
print(pic_arr.shape)
# previous diffrent is while open with Image function is no pixel scale
# if you open with matplotlib.pyplot.imshow it will open with along with pixel scale
# please notice here axis not like regular axis, it's not start with both axis at zero point
print(type(pic_arr))
pic_red = pic_arr.copy()
plt.imshow(pic_red)
pic_red.shape
pic_red[:,:,0] # values are changed
# This will access the
# red cahnnel notice the vals are not btween 0&255
plt.imshow(pic_red[:,:,0])
plt.imshow(pic_red[:,:,1]) # 1 --> indicates the green channels
plt.imshow(pic_red[:,:,2]) # 2 --> indicates the blue channels
# here explicitly we mentioning color mapping is black and white
plt.imshow(pic_red[:,:,0],cmap='gray')
#Bw,camp is colormap, gray scale
# 0 mean no red,pure black
# notice the color of the ears of the dog,no red, they are light red channel values r btween 0-255
plt.imshow(pic_red)
#notice the color of the dog,slightly brownish read
# ax,fig = plt.subplot(121)
plt.figure(figsize=(15,15))
plt.imshow(pic_red[:,:,1]) # thir
plt.show()
plt.figure(figsize=(15,15))
plt.imshow(pic_red[:,:,-1]) # thir
plt.show()
# This is for green,notice the ears, green is gone, they are slight dark
# This is for green channel
plt.imshow(pic_arr[:,:,2])
pic_red[:,:,1] = 0
pic_red.shape
print(pic_red.shape)
plt.imshow(pic_red)
# notice it looks purplsh, why, only b & r rleft
# take out the blue color
pic_red[:,:,2] = 0
pic_red.shape
# notice nothing is showing here without colorchannel
plt.imshow(pic_red)
# take out the blue color
# Their is only three color channels cv2 i.e BGR B-0,G-1,R-2 if make zero a
pic_red[:,:,0] = 0
pic_red.shape
# notice it is redhs
plt.imshow(pic_red)
import numpy as np
import cv
import matplotlib.pyplot as plt
# get_ipython().magic('%matplotlib inline')
import cv2
img = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/00-puppy.jpg')
img.shape
plt.imshow(img)
# notice slightly bluish, why
OCV & matplotlib have defferent format of RGB channels
The image has been correctly loaded by opencv as a numpy arr, but hte color of each pixel has been sordted as BGR
matplotlib's plot expectes an RGB image so, for a correct display of the image, # swap those channels
This can be done by using openCV conversion function cv2.cvtColot() or by working directly with the numpy array.
# This will convert to BGR to RGB color format
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()
img_gray = cv2.imread('c:/Users/hari/desktop/datasets/Naresh_datasets/paret01.jpg')
# img_gray
plt.imshow(img_gray)
img_gray = cv2.imread('c:/Users/hari/desktop/datasets/Naresh_datasets/00-puppy.jpg')
plt.imshow(img_gray)
# why it is not in Bw, becasueof the order of channels
cv2.imread?
img_gray = cv2.imread('c:/Users/hari/desktop/datasets/Naresh_datasets/00-puppy.jpg',
cv2.IMREAD_GRAYSCALE)
plt.imshow(img_gray)
plt.imshow(img_gray,cmap='gray') #cmap = 'gray' means convert to Black white image
plt.imshow(img_gray,cmap='magma')
plt.show()
print(img_gray.min())
print(img_gray.max())
plt.imshow(img_rgb) # this is the original img
plt.show()
img_rgb.shape
img = cv2.resize(img_rgb,(1000,400)) # resize it, this squeeze it
plt.imshow(img) # notice X-axis,y-axis change as per above mentioned
img = cv2.resize(img_rgb,(1375,275))
plt.imshow(img)
w_ratio = 0.8
h_ratio = 0.2
img = cv2.resize(img_rgb,(0,0),img,w_ratio,h_ratio) # img --> is the destination name
plt.imshow(img) # 80% smaller
w_ratio = 0.5
h_ration = 0.5
img_new = cv2.resize(img_rgb,(0,0),img_new,w_ratio,h_ratio)
plt.imshow(img_new) # 50% smaller than originall image
print(img_new.shape)
print(img_rgb.shape)
get_ipython().magic('matplotlib inline')
# along centeral axis
new_img = cv2.flip(img_new,0) # 0 is harizontal axis
plt.imshow(new_img)
# along centeral axis
new_img = cv2.flip(img_new,1) # 1 is vartical axis
plt.imshow(new_img)
new_img1 = cv2.flip(img_new,-1)
plt.imshow(new_img1)
type(new_img1)
cv2.imwrite('my_first_img_writing.png',new_img1)
The above stored the BGR version of the image Larger Display in the Notebook,as the Noteboook displays smapller one by default
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax1 = fig.add_subplot(122)
ax.imshow(new_img1)
ax1.imshow(img_rgb)
fig = plt.figure(figsize=(2,2))
ax = fig.add_subplot(111)
ax.imshow(img_new)
Till now worked function are
import os
import cv2
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/00-puppy.jpg',
cv2.IMREAD_GRAYSCALE)
while True:
# Show the image with opencv
cv2.imshow('MyPuppy',img)
# Wait for sometime on keyboard to be pressed to close window
# Wait for 1ms & if ESC key is pressed
if (cv2.waitKey() & 0xFF == 27):
break
cv2.destroyAllWindows()
help(cv2)
Dt: 16/12/2019
import numpy as np
import cv
blank_img = np.zeros(shape=(512,512,3),dtype=np.int16)
#give shape 512,512
blank_img
print(blank_img.shape)
# Notice here 0s -> is black and 1 is white
from matplotlib import pyplot as plt
%matplotlib inline
plt.imshow(blank_img)
print(blank_img.shape)
img is image *pt1 Vertex of the rectagle
pt2 vertex of the rectangel opposite to pt1
color: rectangle color or brightness(grayscaleimge)
a filled rectange
line Type : Type of line see: type of lines
shift numbers: shift number of fractional bits in the points
pt1 = top left corner,x1,y1
pt2 = bottom right corner x2,y2
Here we are giving two poinst
cv2.rectangle(blank_img,pt1 = (384,0),pt2 =(512,128),
color=(0,255,0),thickness = 5)
plt.imshow(blank_img)
# See here the both ponts are the opposite corners is point(512 on the x-axis,128 -> on y-axis )
cv2.rectangle(blank_img,pt1 = (384,0),pt2 =(512,128),
color=(0,255,0),thickness = 10)
plt.imshow(blank_img)
cv2.rectangle(blank_img,pt1 = (384,0),pt2 =(512,128),
color=(0,255,0),thickness = 20)
plt.imshow(blank_img)
cv2.rectangle(blank_img,pt1=(200,200),pt2 = (300,300),color = (0,255,0),thickness=5)
plt.imshow(blank_img)
cv2.circle(img=blank_img,center=(100,100),radius=50,
color=(255,0,0),thickness=8)
plt.imshow(blank_img)
#filled in
cv2.circle(img=blank_img,center=(100,400),radius=20,color=(255,0,0),thickness=-1)
plt.imshow(blank_img)
# notice here thickness is zero, then it filled by color
cv2.line(blank_img,pt1 = (0,0),pt2 = (511,511),
color = (102,255,255),thickness = 5)
plt.imshow(blank_img)
n_img = cv2.imread('C:/Users/Hari/Desktop/datasets/Naresh_datasets/paret01.jpg')
font = cv2.FONT_HERSHEY_PLAIN
cv2.putText(n_img,text = 'Hello hari',org=(10,500),fontFace = font,
fontScale=10,color = (255,0,255),thickness=5,
lineType = cv2.LINE_AA)
plt.imshow(n_img)
blank_img = np.zeros(shape=(512,512,3),dtype = np.int32)
# Creating the black color sample image
vertices = np.array([[100,300],[200,200],[400,300],[200,400]],np.int32)
# Create vertice for polygon
vertices
vertices.shape
pts = vertices.reshape((-1,1,2 )) # add third dimension i.e 1
cv2.polylines(blank_img,[pts],isClosed=True,color=(255,0,0),thickness=5)
plt.imshow(blank_img)
Direct Drawing with mouse dynamically not statically(physically) Might be sould run as .py script
import cv2
import numpy as np
# Creat a function based on a cv2 event(left botton click)
def draw_circle(event,x,y,flags,param):
pass # this function nothing will do here
# This names the window so we can reference it
cv2.namedWindow(winname='my_art')
# Connect mouse botton to our callback function
cv2.setMouseCallback('my_art',draw_circle)
# Create a blank image
img = np.zeros((512,512,3),np.uint8)
# Runs forever until we break with ESC key on keyboard
while True:
# show the image window
cv2.imshow('my_art',img)
if cv2.waitKey(20) & 0xFF == 27:
break
# Once the script is done, its usally good to call this ,it closes
# all windows (just in case you have multiple windows called)
cv2.destroyAllWindows()
#ESC to close
import numpy as np
import cv2
# function is identical the creating image and
def draw_circle(event,x,y,flags,param): # x,y are passed from setmousecallback
if event == cv2.EVENT_LBUTTONDOWN: # when the left mouse is clicked down
cv2.circle(img,(x,y),100,(0,255,0),-1)
#***Below lines will create the square image and windows and MOuse call back function
#create a black image
img = np.zeros((512,512,3),np.uint8)
# this name is the window so we can reference it
cv2.namedWindow(winname='my_drawing')
# connects the mouse bottn to our callback function
cv2.setMouseCallback('my_drawing',draw_circle)
#*****This while will continue until getting respones as ESC key from the user******
while True: #Runs forever until we break with Esc key on keyboard
# Shows the image window
cv2.imshow('my_drawing',img)
if cv2.waitKey(20) & 0xFF == 27:
break
# Once script is done, its usually good practice to call this line
# It closes all windows (just in case you have multiple windows called)
cv2.destroyAllWindows()
# V3
# ## Adding Functionality with Event Choices
import cv2
import numpy as np
# Create a function based on a CV2 Event (Left button click)
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),100,(0,255,0),-1)
elif event == cv2.EVENT_RBUTTONDOWN:
cv2.circle(img,(x,y),100,(0,0,255),-1)
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# This names the window so we can reference it
cv2.namedWindow(winname='my_drawing')
# Connects the mouse button to our callback function
cv2.setMouseCallback('my_drawing',draw_circle)
while True: #Runs forever until we break with Esc key on keyboard
# Shows the image window
cv2.imshow('my_drawing',img)
if cv2.waitKey(20) & 0xFF == 27: # ESC
break
cv2.destroyAllWindows()
import cv2
import numpy as np
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN: # add a event to the down[LEFT click DOWN]
cv2.circle(img,(x,y),100,(0,255,0,255),-1)
# img is image,(x,y is coordinate capture by the mouse events),100 ---> radius in pixel
#color param(0,255,0), -1 --> represent the filling color
elif event == cv2.EVENT_RBUTTONDOWN:
cv2.circle(img,(x,y),100,(0,0,255),-1)
# second block create black image
img = np.zeros((512,512,3),np.uint8)
# named window reference
cv2.namedWindow(winname='my_art')
cv2.setMouseCallback('my_art',draw_circle)
while True:
cv2.imshow('my_art',img)
if cv2.waitKey(20) & 0xFF== 27:
break
cv2.destroyAllWindows()
import numpy as np
import cv2
# create a function based on cv2 event(Left botton click)
drawing = False
ix,iy = -1,-1 # to keep track
# Mouse callback function
def draw_rectangle(event,x,y,flags,param):
global drawing,ix,iy,mode
# create a rectange mouse event
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
# after we have to track the mouse points
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
# Now the mouse is moving
if drawing == True:
# if drawing is true it means you've already clicked on the left botton on mouse
ix,iy = x,y
# we draw a rectangle from the previous poistion x,y
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
# ix,iy is the starting points and x,y is end points(x-coordinate,y-coordinate)
# color- is color representation i.e green
# -1 === represent the filling the color
elif event == cv2.EVENT_LBUTTONUP:
# once you lift the mouse left botton,drawing is need to finished
drawing = False
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
# Recall above defined function for drawing a rectange
img = np.zeros((512,512,3),np.uint8)
cv2.namedWindow(winname='my_art2')
cv2.setMouseCallback('my_art2',draw_rectangle)
while True:
cv2.imshow('my_art2',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
img1 = cv2.imread('c:/Users/Hari/Desktop/Hari.jpg')
plt.imshow(img1)
cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # GBR to RGB
plt.imshow(img)
img3 = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV) # BGR to HSV
plt.imshow(img3)
img2 = cv2.cvtColor(img1,cv2.COLOR_BGR2HLS) # BGR to HLS
plt.imshow(img2)
# two images
img01 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/dog_backpack.png')
plt.imshow(img01)
img02 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/watermark_no_copy.png')
plt.imshow(img02)
img01 = cv2.cvtColor(img01,cv2.COLOR_BGR2RGB)
plt.imshow(img01)
print(img01.shape)
img02 = cv2.cvtColor(img02,cv2.COLOR_BGR2RGB)
plt.imshow(img02)
print(img02.shape)
img1_s = cv2.resize(img01,(1200,1200))
plt.imshow(img1_s)
img2_s = cv2.resize(img02,(1200,1200))
plt.imshow(img2_s)
print(img01.shape)
print(img02.shape)
src1 = img1_s,alpha = 0.7,src2 = img2_s,beta = 0.5,gamma = 5
blended = cv2.addWeighted(src1 = img1_s,alpha = 0.7,src2 = img2_s,beta = 0.5,gamma = 5)
plt.imshow(blended)
plt.show()
src1 = img1_s,alpha = 0.8,src2 = img2_s,beta = 0.1,gamma = 0.5
blended1 = cv2.addWeighted(src1 = img1_s,alpha = 0.8,src2 = img2_s,beta = 0.1,gamma = .5)
plt.imshow(blended)
plt.show()
blended1 = cv2.addWeighted(src1 = img1_s,alpha = 0.8,src2 = img2_s,beta = 0.1,gamma = 10)
plt.imshow(blended) # Light projection
plt.show()
dt: 20/12/2019
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
im1 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/dog_backpack.jpg')
im2 = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/watermark_no_copy.png')
im2 = cv2.resize(im2,(600,600))
im1 = cv2.cvtColor(im1,cv2.COLOR_BGR2RGB)
im2 = cv2.cvtColor(im2,cv2.COLOR_BGR2RGB)
print(plt.imshow(im1))
# plt.imshow(im2)
plt.imshow(im2)
# create a region of interset
print(im1.shape)
print(im2.shape)
# we can take any coordinates
x_offest = 934 - 600 # 934 is the width of larger image and 600 is width of smaller image
y_offest = 1401 - 600 # 1401 is the height of the larger image
y_offest
print(im2.shape)
# createing an ROI of the same size of the foreground image
# (smaller image that will go on top)
rows,cols,channels = im2.shape # tuple unpacking
# create ROI
roi = im1[y_offest:1401,x_offest:943] # this the bottom right hand corner
roi.shape
plt.imshow(roi)
# Next we create a mask
# create a mask of logo and create its inverse mask also
im2gray = cv2.cvtColor(im2,cv2.COLOR_BGR2GRAY) # conversion image
plt.imshow(im2gray)
display(im2gray.shape)
plt.imshow(im2gray,cmap='gray') # orginal image
# inverse the colors
mask_inv = cv2.bitwise_not(im2gray) # just inverse of the yellow colored image
plt.imshow(mask_inv)
cv2.imwrite('mask_inverse.png',mask_inv)
# convert black and white
plt.imshow(mask_inv,cmap='gray')
mask_inv.shape # color channel is removed we already know that b/w image does't have color channels
# convert mask to have three channles
import numpy as np
white_background = np.full(im2.shape,255,dtype = np.uint8)
plt.imshow(white_background) # notice here it's white image, we override with white color
# bitwise_or operation
bk = cv2.bitwise_or(white_background,white_background,mask = mask_inv)
# parameter for bitwize_or # bitwise_or(src1, src2[, dst[, mask]]) -> dst
plt.imshow(bk)
bk.shape
## Grab Original Foreground image and place on top of mask
plt.imshow(mask_inv,cmap='gray')
fg = cv2.bitwise_or(im2,im2,mask=mask_inv)
plt.imshow(fg)
fg.shape # three channles image
# Get ROI and blend in the mask with the ROI
print(roi.shape)
plt.imshow(roi)
final_roi = cv2.bitwise_or(roi,fg)
plt.imshow(final_roi)
cv2.imwrite('btwise_or.png',final_roi)
# checking the functionality
final_roi1 = cv2.bitwise_and(roi,fg)
plt.imshow(final_roi1)
cv2.imwrite('btwise_and.png',final_roi1)
# checking the functionality
final_roi2 = cv2.bitwise_xor(roi,fg)
plt.imshow(final_roi2)
cv2.imwrite('btwise_xor.jpg',final_roi2)
# Now add the rest of the image
large_image = im1
small_image = final_roi
large_image[y_offest:y_offest + small_image.shape[0],x_offest:x_offest + small_image.shape[1]] = small_image
plt.imshow(small_image)
plt.imshow(large_image)
plt.show()
# *** Image thresholding *** #
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
img = cv2.imread('c:/users/hari/Desktop/datasets/Naresh_datasets/rainbow.jpg')
plt.imshow(img)
plt.show()
print('shape before removeing zero flag',img.shape)
# Adding the 0 flag to read it in black and white
img = cv2.imread('c:/users/hari/Desktop/datasets/Naresh_datasets/rainbow.jpg',0) # makeing color channel as zero, no color channel mean b/w
plt.imshow(img,cmap='gray')# this is gray scale image
plt.show()
print('shape after removeing zero flag/removeing color channel',img.shape)
**For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value.**
# ## Different Threshold Types
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
#******************
#threshold(src, thresh, maxval, type[, dst]) -> retval, dst
#******************
# thresholding is where certain vals r cutoff ie lt r assigned below 0
# and others abv r 1
ret
# 127 is min
# 255 is max pssbl thrshld
img.max() # this max val in the img
display(ret)
plt.imshow(thresh1,cmap='gray')
plt.show()
# this is binary img, all r either 0 or 255
# 0 is blk
print('thresolt: ',ret)
print('max values pixel: ',thresh1.shape)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
plt.imshow(thresh2,cmap='gray')
plt.show()
# ### Threshold Truncation
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
plt.imshow(thresh3,cmap='gray')
plt.show()
# ### Threshold to Zero
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
plt.imshow(thresh4,cmap='gray')
plt.show()
# ### Threshold to Zero (Inverse of abv)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
plt.imshow(thresh5,cmap='gray')
plt.show()
The function transforms a grayscale image to a binary image according to the formulae:
THRESH_BINARY dst(x,y) = \fork{\texttt{maxValue}}{if $src(x,y) > T(x,y)$}{0}{otherwise}
THRESH_BINARY_INV dst(x,y) = \fork{0}{if $src(x,y) > T(x,y)$}{\texttt{maxValue}}{otherwise}
# # Real World Applications
# ## Adaptive Thresholding
# ### Sudoku Image
img = cv2.imread("c:/Users/Hari/Desktop/datasets/Naresh_datasets/crossword.jpg",0)
plt.imshow(img)
plt.show()
# After removing the color channels
plt.imshow(img,cmap='gray')
plt.show()
# To
def show_pic(img):
fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(111)
ax.imshow(img,cmap='gray')
show_pic(img)
print(img.shape)
# ## Simple Binary
ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
show_pic(th1)
display(th1.shape)
#v loose sm of the quality, sm of the gray spaces get thrlded to white
# but v want them to keep as blk
# image quality is dropped, i.e below thersold 127 pixel value set to zero
# incr the thrshld
ret,th1 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)
show_pic(th1)
we used one global value as a threshold. But this might not be good in all cases, e.g. if an image has different lighting conditions in different areas. In that case, adaptive thresholding can help.*
Whereas the conventional thresholding operator uses a global threshold for all pixels, adaptive thresholding changes the threshold dynamically over the image. This more sophisticated version of thresholding can accommodate changing lighting conditions in the image, e.g. those occurring as a result of a strong illumination gradient or shadows.
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,8)
# it will look around the neighborhood of pixl vals
# the blk squares r not filled in due to thrshld/cutoff unlike binary thrsld
# Play with last 2 numbers
show_pic(th2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,15,8)
show_pic(th3)
blended = cv2.addWeighted(src1=th1,alpha=0.7,src2=th2,beta=0.3,gamma=0)
show_pic(blended)
# this is a blend of dfrnt thrshlods
Image blurring is achieved by convolving the image with a low-pass filter kernel. It is useful for removing noise. It actually removes high frequency content (e.g: noise, edges) from the image resulting in edges being blurred when this is filter is applied. (Well, there are blurring techniques which do not blur edges). OpenCV provides mainly four types of blurring techniques.
# ## Convenience Functions
# function for loading the puppy image.
import warnings
warnings.filterwarnings('ignore')
def load_img():
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/bricks.jpg').astype(np.float32) / 255
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img
def display_img(img):
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax.imshow(img)
i = load_img()
display_img(i)
img = load_img()
gamma = 1/4 #gamma is 0.25
effected_image = np.power(img, gamma)
display_img(effected_image)
# Gamma Corr is abt increasing/decreasing the brightness effect
# the img is brighter if gamma < 1
gamma = 1/10 # gamma = 0.1
effected_image = np.power(img, gamma)
display_img(effected_image)
# img fades more
gamma = 2 # gamma is not decimal so it is higher brightness
effected_image = np.power(img, gamma)
display_img(effected_image)
# img fades more
gamma = 8
effected_image = np.power(img, gamma)
display_img(effected_image)
# almost black
# Blurring
# ### Low Pass Filter with a 2D Convolution
# A fitlering operation known as 2D convolution can be used to create
# a low-pass filter.
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
#org is the position
# setup the K for LPF
# ### Create the Kernel
1/25 # 0.04
kernel = np.ones(shape=(5,5),dtype=np.float32)/25
kernel
dst = cv2.filter2D(img,-1,kernel)
# -1 is neg depth
display_img(dst)
# ntc the spacing in letters is pink & filled
# bck to orgnl img
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
# this is the orgnl img
# cv2.blur(src, ksize[, dst[, anchor[, borderType]]])
blurred_img = cv2.blur(img,ksize=(5,5)) # this is default cv2s built in kernel
display_img(blurred_img)
blurred_img = cv2.blur(img,ksize=(10,10)) # this is default cv2s built in kernel
display_img(blurred_img)
# ## Gaussian Blurring
# tks a group of pxls calc avg or median then mk them the outputs
# this the fresh img
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
# cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
blurred_img = cv2.GaussianBlur(img,(5,5),10)
display_img(blurred_img)
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
median = cv2.medianBlur(img,5)
display_img(median)
# ntc k in bricks it is not tht much blurrd as in gaussian blur
# it is more clear this is removing noise from text
# a more useful case of Median Blurring by adding some random noise to an image.
img = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/sammy.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
display_img(img)
print(img.max())
print(img.min())
print(img.mean())
print(img.shape)
# by default image is noisey only
noise_img = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/sammy_noise.jpg') # this is noisy img
display_img(noise_img)
median = cv2.medianBlur(noise_img,5) # we achived better image from noise image
display_img(median)
# this looks btr than the noisy img although not as clear as the org img
img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
# cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
cv2.putText(img,text='bricks',org=(10,600), fontFace=font,fontScale= 10,color=(255,0,0),thickness=4)
display_img(img)
As we noted, the filters we presented earlier tend to blur edges. This is not the case for the bilateral filter, cv2.bilateralFilter(), which was defined for, and is highly effective at noise removal while preserving edges. But the operation is slower compared to other filters. We already saw that a Gaussian filter takes the a neighborhood around the pixel and finds its Gaussian weighted average. This Gaussian filter is a function of space alone, that is, nearby pixels are considered while filtering. It does not consider whether pixels have almost the same intensity value and does not consider whether the pixel lies on an edge or not. The resulting effect is that Gaussian filters tend to blur edges, which is undesirable.
blur = cv2.bilateralFilter(img,9,75,75)
display_img(blur)
# the img has blurrd but the txt is slightly clear
# it is in between median blur & mean
# the obj is to reduce the noise or the detail in the img
def load_img():
blank_img =np.zeros((600,600))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_img,text='ABCDE',org=(50,300), fontFace=font,fontScale=5,color=(255,255,255),thickness=25,lineType=cv2.LINE_AA)
return blank_img
def display_img(img):
fig = plt.figure(figsize=(6,5))
ax = fig.add_subplot(111)
ax.imshow(img,cmap='gray')
img = load_img()
display_img(img)
all the pixels near boundary will be discarded depending upon the size of kernel. So the thickness or size of the foreground object decreases or simply white region decreases in the image. It is useful for removing small white noises (as we have seen in colorspace chapter), detach two connected objects etc.
# ## Erosion
# Erodes away boundaries of foreground objects.
# Works when foreground is light color (preferrably white) and background is dark.
kernel = np.ones((5,5),np.uint8)
# create the kernel
erosion1 = cv2.erode(img,kernel,iterations = 1)
display_img(erosion1)
# FG is white, BG is black
# ntc the cnnection btw A & B is slightly weak compared to the orgnl
img = load_img()
kernel = np.ones((5,5),np.uint8)
erosion5 = cv2.erode(img,kernel,iterations = 4)
display_img(erosion5)
# cnnctns r getting eroded away
# ## Opening
# Opening is erosion followed by dilation. Used in removing background noise!
img = load_img()
white_noise = np.random.randint(low=0,high=2,size=(600,600))
white_noise
# arr of pts 0 & 1 of size 600 x 600
display_img(white_noise)
img.max()
# ntc 0s and 1s
# now add the white noise into our txt img
white_noise = white_noise*255
white_noise
# this is the same scale of orgl img
# now add the white noise into our txt img
white_noise = white_noise*255
white_noise
# this is the same scale of orgl img
display_img(white_noise)
# now add the white noise into our txt img
noise_img = white_noise+img
display_img(noise_img)
# img of dark bg & light fg w a lot of noise
Morphological operators often take a binary image and a structuring element as input and combine them using a set operator (intersection, union, inclusion, complement). They process objects in the input image based on characteristics of its shape, which are encoded in the structuring element. The mathematical details are explained in Mathematical Morphology.
opening = cv2.morphologyEx(noise_img, cv2.MORPH_OPEN, kernel,)
display_img(opening)
# ntc v r able to remove the noise wo distorting the orgnl img
# this is for remving the bg noise
#if v now see the orgl img
display_img(img)
# dilation is expanding on img
# ### Closing
# Useful in removing noise from foreground objects, as black dots on top of the
# white text.
# ### Closing
# Useful in removing noise from foreground objects, as black dots on top of the
# white text.
img = load_img() # reload/reset the img
display_img(img)
black_noise = np.random.randint(low=0,high=2,size=(600,600))
black_noise
display_img(black_noise) # looks same as white noise
black_noise= black_noise * -255
black_noise # ntc the neg vals it will not affect the black but white fg
display_img(black_noise) # ntc the white fg is filled with noise
# add blck nose to the img
black_noise_img = img + black_noise
display_img(black_noise_img)
display_img(noise_img) # this is white noise
#black_noise = np.random.randint(low=0,high=2,size=(600,600))
#black_noise
#display_img(black_noise_img)
# v will try to mk the neg vals to zeros, as the pxls r from 0 to 255
black_noise_img[black_noise_img==-255] = 0
black_noise_img
display_img(black_noise_img)
# closing is a process to clean up the FG
# opening is a process to clean up he BG
closing = cv2.morphologyEx(black_noise_img, cv2.MORPH_CLOSE, kernel)
display_img(closing)
# ## Morphological Gradient
# Difference between dilation and erosion of an image.
img = load_img()
display_img(img)
# erosion tries to remove the edges of alphabet and blck bg
# dilation tries to add around the edges and mkae it more bubbly
# gradient will tk the dfrnt btw the 2
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
display_img(gradient)
# this is a simple process of edge detection
# this is the dfrnt btw erosion & dilation
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sudoku.jpg',0)
def display_img(img):
fig = plt.figure(figsize=(6,5))
ax = fig.add_subplot(111)
ax.imshow(img,cmap='gray')
display_img(img)
# this is x gradeint Sobel
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
# cv2.CV_64F is pxl precision
# 1 is derivative x (dx)
# 0 is derivative y (dy)
# ksize is an odd num v can chang this
display_img(sobelx)
# vert lins r clear
# it ds not erase any hor lines
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
display_img(sobely)
# ntc hor lines r clear
# this is the laplacean of the img
# 2nd der of x & 2nd der of y
laplacian = cv2.Laplacian(img,cv2.CV_64F)
display_img(laplacian)
# ntc it tries to do edge detection wrt to x & y
# it gets rid of sm noise
# ntc the edges around 8 went frm white to black to white
# nd the nums r clear
# ex of an appli whi will look at a sudoku pic
# nd auto fill in sm nums
later some more things need to be practice but now time left to practice, i will it later
blended = cv2.addWeighted(src1=sobelx,alpha = 0.5,src2 = sobely,beta = 0.5,gamma=0)
display_img(blended)
display(blended.shape)
# this blended result of xgrad,ygrad,edges are more clear
# we can also combine thersolding & morphological oerators
# using morp oper
# ### Morphological Operators
kernel = np.ones((4,4),np.uint8)
gradient = cv2.morphologyEx(blended,cv2.MORPH_GRADIENT,kernel)
display_img(gradient)
# this gvs b & w result
# this is used for edge detection
# In edge detection all the techniqs r combined and called
this is used for edge detection In edge detection all the techniqs r combined and called
# more combinations
# Try it on laplacian result
kernel = np.ones((3,3),np.uint8)
gradient = cv2.morphologyEx(blended,cv2.MORPH_GRADIENT,kernel)
display_img(gradient)
# ### Thresholds
ret,th1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
display_img(th1)
ret,th1 = cv2.threshold(gradient,200,255,cv2.THRESH_BINARY_INV)
display_img(th1)
ret,th1 = cv2.threshold(blended,100,255,cv2.THRESH_BINARY_INV)
display_img(th1)
#Orginal image
dark_horse = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/horse.jpg')
plt.imshow(dark_horse)
# its repesent exact releastic image in RGB mode
show_horse = cv2.cvtColor(dark_horse,cv2.COLOR_BGR2RGB)
plt.imshow(show_horse)
rainbow= cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/rainbow.jpg')
plt.imshow(rainbow)
show_rainbow = cv2.cvtColor(rainbow,cv2.COLOR_BGR2RGB)
# display(show_rainbow)
plt.imshow(show_rainbow)
blue_bricks = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/bricks.jpg')
plt.imshow(blue_bricks)
plt.show()
# ### OpenCV Histogram
# **cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])**
# * images : it is the source image of type uint8 or float32.
# it should be given in square brackets, ie, “[img]”.
# * channels : it is also given in square brackets.
# It is the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively.
# * mask : mask image.
# To find histogram of full image, it is given as “None”. But if you want to find histogram of particular region of image, you have to create a mask image for that and give it as mask. (I will show an example later.)
# * histSize : this represents our BIN count.
# Need to be given in square brackets. For full scale, we pass [256].
# * ranges : this is our RANGE. Normally, it is [0,256].
hist_values = cv2.calcHist([blue_bricks],channels =[0],histSize=[256],mask=None,ranges=[0,256])
hist_values.shape
plt.imshow(hist_values)
plt.show()
plt.imshow(show_horse)
plt.show()
plt.imshow(dark_horse)
plt.show()
hist_values = cv2.calcHist([dark_horse],channels=[0],mask=None,histSize=[256],ranges=[0,256])
plt.plot(hist_values)
plt.show()
# ## Plotting 3 Color Histograms
img = blue_bricks
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.title('Blue Bricks Image')
plt.show()
plt.imshow(img)
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,50])
plt.ylim([0,500000])
plt.title('Dark Horse')
plt.show()
plt.imshow(img)
img = rainbow
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.title('Rainbow Image')
plt.show()
plt.imshow(img)
# ### Masking
# We can mask only certain parts of the image.
img = rainbow
img.shape
# create a mask
img.shape[:2]
# same as above
mask = np.zeros(img.shape[:2], np.uint8)
mask
plt.imshow(mask)
plt.show()
plt.imshow(mask,cmap='gray')
plt.show()
mask[300:400, 100:400] = 255
# slice a rect from the img
# then set it to 255 whi is wht
plt.imshow(mask,cmap='gray')
plt.imshow(show_rainbow)
# this is color corrected
masked_img = cv2.bitwise_and(img,img,mask = mask)
# this is used for hist calc
# create a mask in the rainbow
# this is used for rgb color calc
show_masked_img = cv2.bitwise_and(show_rainbow,show_rainbow,mask = mask)
plt.imshow(show_masked_img)
# ntc not much red is here cmprd to orgl img
hist_mask_values_red = cv2.calcHist([rainbow],channels=[2],mask=mask,histSize=[256],ranges=[0,256])
# rainbow is orgnl img
# 2 is red channl in b g r
# mask is the obj v created on top
plt.plot(hist_mask_values_red)
plt.title('Histogram for RED values for the Masked Area')
# v low vals for red & most of the vals r 0
plt.imshow(show_rainbow)
plt.show()
# ntc a lot of red
hist_full_values_red = cv2.calcHist([rainbow],channels=[2],mask=None,histSize=[256],ranges=[0,256])
plt.plot(hist_full_values_red)
plt.title('Histogram for RED values of the full image')
plt.show()
# ntc a lot of vals for red
## Histogram Equalization
gorilla = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/gorilla.jpg',0)
def display(img,cmap=None):
fig = plt.figure(figsize=(5,4))
ax = fig.add_subplot(111)
ax.imshow(img,cmap)
display(gorilla)
# Calc histogram, then equalize it, then visualize the dfrnc, then run on color version
hist_values = cv2.calcHist([gorilla],channels=[0],mask=None,histSize=[256],ranges=[0,256])
# Calc histogram, then equalize it, then visualize the dfrnc, then run on color version
hist_values = cv2.calcHist([gorilla],channels=[0],mask=None,histSize=[256],ranges=[0,256])
# channels[0] is 0 coz it is gray there is only one channel
plt.plot(hist_values)
plt.show()
# looks like v hv lot of lighter color and few darker but not much black
# if v look at the img the lighter colors r coming frm the rock
# bhnd gorilla and gor is also a little lighter nd not compleely dark
# the peak is the lighter gray and as v mv to rhs is the darker colr
# this is a ver of gor
eq_gorilla = cv2.equalizeHist(gorilla)
display(eq_gorilla,cmap='gray')
# ntc the contrast has increased
# the lighter vals now r min vals of 0
# nd the darker vals r black whi r the near the hair of gor
# nd the edges of rock
hist_values = cv2.calcHist([eq_gorilla],channels=[0],mask=None,histSize=[256],ranges=[0,256])
plt.plot(hist_values)
# ntc a lot of spikes going down coz of 0 vals
# a lot of lighter vals hv cm 0
# whereas the orgnl one is more flat
# ## redo the abv for Color Image
color_gorilla = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/gorilla.jpg')
display(color_gorilla)
show_gorilla = cv2.cvtColor(color_gorilla,cv2.COLOR_BGR2RGB)
display(show_gorilla)
# Convert to HSV colorspace to equlaize a color img
# equzli increass the color contrast
# hue sat val
hsv = cv2.cvtColor(color_gorilla, cv2.COLOR_BGR2HSV)
display(show_gorilla)
# Grab V channel
hsv[:,:,2]
# vals chnnl is wht v r interested in
# 0 chnnl is the hue
# 1 chnnl s the sat
hsv[:,:,2].max() # max val
hsv[:,:,2].min()
# equalize val chnnl, then replace the orgnl vals
hsv[:,:,2] = cv2.equalizeHist(hsv[:,:,2])
# Convert back hsv to RGB to visualize
eq_color_gorilla = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
display(eq_color_gorilla)
# compare this to orgnl v hv higher contrast
# ntc the rock bhnd is much lighter compared to the orgnl
# *** Video Processing
# when running video files mk sure only 1 kernel is running
# connecting to the camera
import cv2
# Connects to your computer's default camera
cap = cv2.VideoCapture(0)
# a VC is a series of imgs
# a grp of imgs getting updtd continually
# Get the width and height from video feed
# (returns float which we need to convert to integer for later)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# tuple unpack
# Our operations on the frame come here
# convert it to gray frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
# This command let's us quit with the "q" button on a keyboard.
# Simply pressing X on the window won't work!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture and destroy the windows
cap.release() # stop capturing the video
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter('my_v.mp4',cv2.VideoWriter_fourcc(*'DIVX'),25,(width,height))
while True:
ret,frame = cap.read()
cv2.imshow('frame',frame)
writer.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
writer.release()
cv2.destroyAllWindows()
# # OpenCV with Video Files
# open the recorded video from the last ,
# you can use this code to open any major video format.
# Run everything in one cell!
import cv2
import time
# Same command function as streaming, its just now we pass in the file path, nice!
cap = cv2.VideoCapture('my_v.mp4')
# FRAMES PER SECOND FOR VIDEO
fps = 25
# check if the video was acutally there
# If you get an error at thsi step, triple check your file path!!
if cap.isOpened()== False:
print("Error opening the video file. Please double check your file path for typos. Or move the movie file to the same location as this script/notebook")
# While the video is opened
while cap.isOpened():
# Read the video file.
ret, frame = cap.read()
# If we got frames, show them.
if ret == True:
# Display the frame at same frame rate of recording
#time.sleep(1/fps)
cv2.imshow('frame',frame)
# Press q to quit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Or automatically break this whole loop if the video is over.
else:
break
cap.release()
cv2.destroyAllWindows()
import time
# wht is the dfrnc
cap = cv2.VideoCapture('my_v.mp4')
# fps = 25
fps = 100
if cap.isOpened()== False:
print("Error opening the video file. Please double check your file path for typos. Or move the movie file to the same location as this script/notebook")
while cap.isOpened():
ret, frame = cap.read()
if ret == True:
time.sleep(1/fps)
cv2.imshow('frame',frame)
if cv2.waitKey(100) & 0xFF == ord('q'): # Here waitkey until wait key. frame will run
break
else:
break
cap.release()
cv2.destroyAllWindows()
# # Drawing on Video
# to analyze video using techniques like object detection or facial recognition,
# we want to draw an image on the video, like a box around a face.
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# using // here because Python // allows for int classical division,
# because we can't pass a float to the cv2.rectangle function
# Coordinates for Rectangle, top left corner
x = width//2
y = height//2
# Width and height
w = width//4
h = height//4
# botton right x+w, y+h
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Draw a rectangle on stream
cv2.rectangle(frame, (x, y), (x+w, y+h), color=(0,0,255),thickness= 4)
# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# ## Interactive Drawing on Video
# Create a function based on a CV2 Event (Left button click)
# mouse callback function
def draw_rectangle(event,x,y,flags,param):
global pt1,pt2,topLeft_clicked,botRight_clicked
# get mouse click
if event == cv2.EVENT_LBUTTONDOWN:
if topLeft_clicked == True and botRight_clicked == True:
topLeft_clicked = False
botRight_clicked = False
pt1 = (0,0)
pt2 = (0,0)
if topLeft_clicked == False:
pt1 = (x,y)
topLeft_clicked = True
elif botRight_clicked == False:
pt2 = (x,y)
botRight_clicked = True
# Global vars
pt1 = (0,0)
pt2 = (0,0)
topLeft_clicked = False
botRight_clicked = False
# Connect to the callback
cap = cv2.VideoCapture(0)
# Create a named window for connections
cv2.namedWindow('Test')
# Bind draw_rectangle function to mouse cliks
cv2.setMouseCallback('Test', draw_rectangle)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if topLeft_clicked:
cv2.circle(frame, center=pt1, radius=5, color=(0,0,255), thickness=-1)
#drawing rectangle
if topLeft_clicked and botRight_clicked:
cv2.rectangle(frame, pt1, pt2, (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Test', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
full = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy.jpg')
full = cv2.cvtColor(full,cv2.COLOR_BGR2RGB)
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(full)
face = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy_face.jpg')
face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)
plt.imshow(face)
print('full sammy shape:' ,full.shape)
print('face sammy shape: ',face.shape)
import cv2
# # Template Matching Methods
# **eval()** function
sum([1,2,3])
mystring = 'sum'
eval(mystring)
myfunc = eval(mystring)
myfunc([1,2,3])
height, width,channels = face.shape
print(width)
print(height)
#The full image to search
# large image
full = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/sammy.jpg')
full = cv2.cvtColor(full,cv2.COLOR_BGR2RGB)
# The template to match
#small image
import matplotlib.pyplot as plt
%matplotlib inline
face = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy_face.jpg')
face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)
my_method = eval('cv2.TM_CCOEFF')
res = cv2.matchTemplate(full,face,my_method)
plt.imshow(res)
we are using strings, we'll use the eval() function to convert to function
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR','cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
import matplotlib as mpl
for m in methods:
full_copy = full.copy()
method = eval(m)
res = cv2.matchTemplate(full_copy,face,method)
min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res)
# draw rectangel
if method in [cv2.TM_SQDIFF or cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
height,width,channels = face.shape
# assign bottom right of the rectangle
bottom_right = (top_left[0] + width, top_left[1] + height)
# Draw red rectangle
# plt.figure(figsize=(16,8))
# mpl.rcParams(figsize(16,6))
plt.rcParams['figure.figsize'] = (16,8)
plt.subplot(121)
plt.imshow(res)
plt.title('Result of template matching')
plt.subplot(122)
plt.imshow(full_copy)
plt.title('Detected point')
plt.suptitle(m)
plt.show()
print('\n')
print('\n')
import cv2
from matplotlib import pyplot as plt
%matplotlib inline
flat_chess = cv2.imread('c:/Users/Hari/desktop/datasets/Naresh_datasets/flat_chessboard.png')
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)
# cvLoadImage(flat_chess, CV_LOAD_IMAGE_GRAYSCALE);
plt.imshow(flat_chess)
print('\n')
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)
# gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COL)
plt.imshow(gray_flat_chess)
real_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/real_chessboard.jpg')
real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB)
plt.imshow(real_chess)
plt.imshow(real_chess,cmap='gray')
real_chess_bw = cv2.cvtColor(real_chess,cv2.COLOR_RGB2GRAY)
plt.imshow(real_chess_bw)
gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_real_chess,cmap='gray')
gray_flat_chess
import numpy as np
gray = np.float32(gray_flat_chess)
gray_flat_chess in gray
gray_flat_chess
import numpy as np
gray = np.float32(gray_flat_chess)
# gray_flat_chess in gray
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
plt.imshow(flat_chess)
plt.show()
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_RGB2BGR)
plt.imshow(flat_chess)
plt.show()
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_flat_chess)
# # Corner Detection
# ### The Image Data
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)
plt.imshow(flat_chess)
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_flat_chess,cmap='gray')
_
real_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/real_chessboard.jpg')
real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB)
plt.imshow(real_chess)
gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_real_chess,cmap='gray')
# # Harris Corner Detection
# **cornerHarris Function**
# * src Input single-channel 8-bit or floating-point image.
# * dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same size as src .
# * blockSize Neighborhood size (see the details on #cornerEigenValsAndVecs ).
# * ksize Aperture parameter for the Sobel operator.
# * k Harris detector free parameter. See the formula in DocString
# * borderType Pixel extrapolation method. See #BorderTypes.
# all integers
gray_flat_chess
# Convert Gray Scale Image to Float Values
gray = np.float32(gray_flat_chess)
gray
# Corner Harris Detection
dst = cv2.cornerHarris(src=gray,blockSize=2,ksize=3,k=0.04)
# blksize is the neighborhood size, it detects edges
# ksize is the par for sobel oper, kernel size
# cornerHarris uses these internally
# k is the harris detector free par
dst
# result is dilated for marking the corners, not important to actual corner detection
# this is so we can plot out the points on the image shown
dst = cv2.dilate(dst,None)
# dilate is a morphological oper
dst
# Threshold for an optimal value, it may vary depending on the image.
flat_chess
flat_chess[dst>0.01*dst.max()]=[255,0,0]
# whenever the corner harris is gt 1% of max val
# reassign 1% of max val to color red
# this is for visualziation
flat_chess
plt.imshow(flat_chess)
# ntc all corners r not detected
# try on real chess board
# Convert Gray Scale Image to Float Values
gray = np.float32(gray_real_chess)
# Corner Harris Detection
dst = cv2.cornerHarris(src=gray,blockSize=2,ksize=3,k=0.04)
# result is dilated for marking the corners, not important to actual corner detection
# this is toplot out the points on the image shown
dst = cv2.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
real_chess[dst>0.01*dst.max()]=[255,0,0]
plt.imshow(real_chess)
# more corners on blk pieces r detected
# all the major corners r detected
# the outside corners r not detected still
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2RGB)
plt.imshow(flat_chess)
gray_flat_chess = cv2.cvtColor(flat_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_flat_chess)
corner = cv2.goodFeaturesToTrack(gray_flat_chess,5,0.01,10)
corner = np.int0(corner)
for i in corner:
x,y = i.ravel()
cv2.circle(real_chess,(x,y),3,255,-1)
plt.imshow(real_chess)
goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance[, corners[, mask[, blockSize[, useHarrisDetector[, k]]]]]) -> corners
corners = cv2.goodFeaturesToTrack(gray_flat_chess,64,0.01,10)
print('corners shape is : ',corners.shape)
corners = np.int0(corners);
print('cornershape is : ',corners.shape)
for i in corners:
x,y = i.ravel()
cv2.circle(flat_chess,(x,y),3,255,-1)
plt.imshow(flat_chess)
real_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/real_chessboard.jpg',)
real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB)
gray_real_chess = cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_real_chess)
corners = cv2.goodFeaturesToTrack(gray_real_chess,60,0.9,10)
for i in corners:
x,y = i.ravel()
cv2.circle(real_chess,(x,y),3,255,-1)
plt.imshow(real_chess)
corners = cv2.goodFeaturesToTrack(gray_real_chess,100,0.9,10)
for i in corners:
x,y = i.ravel()
cv2.circle(real_chess,(x,y),3,255,-1)
plt.imshow(real_chess)
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/sammy_face.jpg')
plt.imshow(img)
plt.show()
edges = cv2.Canny(image=img, threshold1=127, threshold2=127)
plt.imshow(edges)
plt.show()
edges = cv2.Canny(image=img, threshold1=0, threshold2=255)
plt.imshow(edges)
plt.show()
med_val = np.median(img)
lower = int(max(0,0.7* med_val))
upper = int(min(255,1.3* med_val))
edges = cv2.Canny(image=img, threshold1=lower , threshold2=upper)
plt.imshow(edges)
blurred_img = cv2.blur(img,ksize=(5,5))
edges = cv2.Canny(image=blurred_img, threshold1=lower , threshold2=upper)
plt.imshow(edges)
print(lower)
print(upper)
flat_chess = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/flat_chessboard.png')
plt.imshow(flat_chess,cmap='gray')
# open cv built in chessboard
found, corners = cv2.findChessboardCorners(flat_chess,(7,7))
if found:
print('OpenCV was able to find the corners')
else:
print("OpenCV did not find corners. Double check your patternSize.")
corners
corners.shape
flat_chess_copy = flat_chess.copy()
cv2.drawChessboardCorners(flat_chess_copy, (7, 7), corners, found)
plt.imshow(flat_chess_copy)
findCirclesGrid(image, patternSize, flags, blobDetector, parameters[, centers]) -> retval, centers . @brief Finds centers in the grid of c
dots = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/dot_grid.png')
plt.imshow(dots)
found,corners = cv2.findCirclesGrid(dots,(10,10),corners,found)
# ## External vs Internal Contours
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/internal_external.png',0)
img.shape
plt.imshow(img,cmap='gray')
contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
type(contours)
print(len(contours))
print(type(hierarchy))
print(hierarchy.shape)
hierarchy
external_contours = np.zeros(img.shape)
external_contours.shape # 652,1080 is same as orignal img
img.shape
plt.imshow(img)
list(range(len(contours)))
# For every entry in contours
for i in range(len(contours)):
if hierarchy[0][i][3] == -1:
cv2.drawContours(external_contours, contours, i, 255, -1)
plt.imshow(external_contours,cmap='gray')
# internal contours r the eyes & smiley & slices of pepperoni
image_internal = np.zeros(img.shape)
# Iterate through list of contour arrays
for i in range(len(contours)):
# If third column value is NOT equal to -1 than its internal
if hierarchy[0][i][3] != -1:
# Draw the Contour
cv2.drawContours(image_internal, contours, i, 255, -1)
plt.imshow(image_internal,cmap='gray')
# slices
image_internal = np.zeros(img.shape)
for i in range(len(contours)):
if hierarchy[0][i][3] == 4:
cv2.drawContours(image_internal, contours, i, 255, -1)
plt.imshow(image_internal,cmap='gray')
image_internal = np.zeros(img.shape)
for i in range(len(contours)):
if hierarchy[0][i][3] == 0:
cv2.drawContours(image_internal, contours, i, 255, -1)
plt.imshow(image_internal,cmap='gray')
import cv2
from matplotlib import pyplot as plt
def display(img,cmap='gray'):
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax.imshow(img,cmap='gray')
reeses = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/reeses_puffs.png')
display(reeses)
reeses = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/reeses_puffs.png',0)
display(reeses)
cereals = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/many_cereals.jpg')
display(cereals)
# this is the target img
cereals = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/many_cereals.jpg',0)
display(cereals)
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(reeses,None)
kp2, des2 = orb.detectAndCompute(cereals,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
sngl_mtch = matches[0]
sngl_mtch.distance
matches = sorted(matches, key = lambda x:x.distance)
matches
len(matches)
reeses_matches = cv2.drawMatches(reeses,kp1,cereals,kp2,matches[:25],None,flags=2)
display(reeses_matches)
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(reeses,None)
kp2, des2 = sift.detectAndCompute(cereals,None)
# first try this
#!conda install -c menpo opencv
# if it ds not work try this
# open Ana prompt as Admin
# pip3 uninstall opencv-contrib-python
# pip install opencv-contrib-python
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
matches
# first match is btr than a 2nd match and so on
# first col is the first best mtch
# sec col is the 2nd best mtch
# if first mtch is close in dstnc to the 2nd mtch then
# overal it is a good feature to mtch on
# also if v hv a strong mtch in the first col
# nd 2nd bst mtch is far away in dstnc
# then this descriptor whi is in the first row
# Apply ratio test
# is used for checking if the 2 mtches r close in dist or not
good = []
for match1,match2 in matches:
if match1.distance < 0.75*match2.distance:
# if m1 dist is lt 75% of mtch2 dist
# then the descriptor is a gd mtch
good.append([match1])
# mtch1 is a ratio of 75% of mtch2 v call it a ratio test
# less dist == btr mtch
print(good)
print(len(good))
# 78 r best mtches
print(matches)
import cv2
# cv2.drawMatchesKnn expects list of lists as matches.
sift_matches = cv2.drawMatchesKnn(reeses,kp1,cereals,kp2,good,None,flags=2)
display(sift_matches)
# # FLANN based Matcher
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(reeses,None)
kp2, des2 = sift.detectAndCompute(cereals,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
good = []
# ratio test
for i,(match1,match2) in enumerate(matches):
if match1.distance < 0.7*match2.distance:
good.append([match1])
flann_matches = cv2.drawMatchesKnn(reeses,kp1,cereals,kp2,good,None,flags=0)
display(flann_matches)
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(reeses,None)
kp2, des2 = sift.detectAndCompute(cereals,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in range(len(matches))]
# ratio test
for i,(match1,match2) in enumerate(matches):
if match1.distance < 0.7*match2.distance:
matchesMask[i]=[1,0]
draw_params = dict(matchColor = (0,255,0),
singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = 0)
flann_matches = cv2.drawMatchesKnn(reeses,kp1,cereals,kp2,matches,None,**draw_params)
display(flann_matches)
import numpy as np
import cv2
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
def display(img,cmap=None):
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax.imshow(img,cmap=cmap)
sep_coins = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/pennies.jpg')
display(sep_coins)
sep_blur = cv2.medianBlur(sep_coins,25)
display(sep_blur)
gray_sep_coins = cv2.cvtColor(sep_blur,cv2.COLOR_BGR2GRAY)
display(gray_sep_coins,cmap='gray')
ret, sep_thresh = cv2.threshold(gray_sep_coins,160,255,cv2.THRESH_BINARY_INV)
display(sep_thresh,cmap='gray')
ret, sep_thresh = cv2.threshold(gray_sep_coins,127,255,cv2.THRESH_BINARY_INV)
display(sep_thresh,cmap='gray')
ret, sep_thresh = cv2.threshold(gray_sep_coins,160,255,cv2.THRESH_BINARY_INV)
display(sep_thresh,cmap='gray')
contours, hierarchy = cv2.findContours(sep_thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
if hierarchy[0][i][3] == -1:
cv2.drawContours(sep_coins, contours, i, (255, 0, 0), 10)
display(sep_coins)
img = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/pennies.jpg')
img = cv2.medianBlur(img,35) # v r using huge kernel size
display(img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
display(thresh,cmap='gray')
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
display(thresh,cmap='gray')
kernel = np.ones((3,3),np.uint8)
kernel
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
display(opening,cmap='gray')
sure_bg = cv2.dilate(opening,kernel,iterations=3)
display(sure_bg,cmap='gray')
sure_bg = cv2.dilate(opening,kernel,iterations=4)
display(sure_bg,cmap='gray')
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
display(dist_transform,cmap='gray')
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
display(sure_fg,cmap='gray')
display(dist_transform,cmap='gray')
sure_fg = np.uint8(sure_fg)
sure_fg
unknown = cv2.subtract(sure_bg,sure_fg)
display(unknown,cmap='gray')
ret, markers = cv2.connectedComponents(sure_fg)
markers
markers = markers+1
markers[unknown==255] = 0
display(markers,cmap='gray')
# #### next Step 10: Apply Watershed Algorithm to find Markers
markers = cv2.watershed(img,markers)
display(markers)
contours, hierarchy = cv2.findContours(markers.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
if hierarchy[0][i][3] == -1:
cv2.drawContours(sep_coins, contours, i, (255, 0, 0), 10)
display(sep_coins)
road = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/road_image.jpg')
road_copy = np.copy(road)
plt.imshow(road)
print(road.shape)
print(road.shape[:2])
marker_image = np.zeros(road.shape[:2],dtype=np.int32)
plt.imshow(marker_image)
segments = np.zeros(road.shape,dtype=np.uint8)
segments.shape
from matplotlib import cm
cm.tab10(0)
cm.tab10(1)
np.array(cm.tab10(0))
np.array(cm.tab10(0))[:3]
np.array(cm.tab10(0))[:3]*255
x = np.array(cm.tab10(0))[:3]*255
tuple(x.astype(int))
def create_rgb(i):
x = np.array(cm.tab10(i))[:3]*255
return tuple(x)
colors = []
for i in range(10):
colors.append(create_rgb(i))
colors
n_markers = 10
current_marker = 1
marks_updated = False
def mouse_callback(event, x, y, flags, param):
global marks_updated
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(marker_image, (x, y), 10, (current_marker), -1)
cv2.circle(road_copy, (x, y), 10, colors[current_marker], -1)
marks_updated = True
cv2.namedWindow('Road Image')
cv2.setMouseCallback('Road Image', mouse_callback)
while True:
cv2.imshow('WaterShed Segments', segments)
cv2.imshow('Road Image', road_copy)
k = cv2.waitKey(1)
if k == 27:
break
elif k == ord('c'):
road_copy = road.copy()
marker_image = np.zeros(road.shape[0:2], dtype=np.int32)
segments = np.zeros(road.shape,dtype=np.uint8)
elif k > 0 and chr(k).isdigit():
current_marker = int(chr(k))
if marks_updated:
marker_image_copy = marker_image.copy()
cv2.watershed(road, marker_image_copy)
segments = np.zeros(road.shape,dtype=np.uint8)
for color_ind in range(n_markers):
segments[marker_image_copy == (color_ind)] = colors[color_ind]
cv2.destroyAllWindows()
import numpy as np
import cv2
import matplotlib.pyplot as plt
# %matplotlib inline # both line interchangeble
get_ipython().magic('matplotlib inline')
nadia = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/Nadia_Murad.jpg',0)
denis = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/Denis_Mukwege.jpg',0)
solvay = cv2.imread('c:/Users/Hari/Desktop/datasets/Naresh_datasets/solvay_conference.jpg',0)
plt.imshow(nadia,cmap='gray')
plt.show()
plt.imshow(denis,cmap='gray')
plt.show()
plt.imshow(solvay,cmap='gray')
plt.show()
face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml')
def detect_face(img):
face_img = img.copy()
face_rects = face_cascade.detectMultiScale(face_img)
for (x,y,w,h) in face_rects:
cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10)
return face_img
result = detect_face(denis)
plt.imshow(result,cmap='gray')
plt.show()
result = detect_face(nadia)
plt.imshow(result,cmap='gray')
plt.show()
result = detect_face(solvay)
plt.imshow(result,cmap='gray')
plt.show()
def adj_detect_face(img):
face_img = img.copy()
face_rects = face_cascade.detectMultiScale(face_img,scaleFactor=1.2, minNeighbors=5)
for (x,y,w,h) in face_rects:
cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10)
return face_img
result = adj_detect_face(solvay)
plt.imshow(result,cmap='gray')
plt.show()
eye_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_eye.xml')
def detect_eyes(img):
face_img = img.copy()
eyes = eye_cascade.detectMultiScale(face_img,scaleFactor=1.2, minNeighbors=5)
for (x,y,w,h) in eyes:
cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10)
return face_img
result = detect_eyes(nadia)
plt.imshow(result,cmap='gray')
eyes = eye_cascade.detectMultiScale(denis)
result = detect_eyes(denis)
plt.imshow(result,cmap='gray')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read(0)
frame = detect_face(frame) # detect_face() function wrote in previos
cv2.imshow('Video Face Detection', frame)
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()
import cv2
face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_smile2.xml')
def detect(gray, frame):
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 22)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 22)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2)
return frame
video_capture = cv2.VideoCapture(0)
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
canvas = detect(gray, frame)
cv2.imshow('Video', canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import cv2
def Cartoon(image_color):
output_image = cv2.stylization(image_color, sigma_s=100, sigma_r=0.3)
return output_image
def LiveCamEdgeDetection_canny(image_color):
threshold_1 = 30
threshold_2 = 80
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(image_gray, threshold_1, threshold_2)
return canny
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # Cap.read() returns a ret bool to indicate success.
cv2.imshow('Live Edge Detection', Cartoon(frame))
#cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_canny(frame))
cv2.imshow('Webcam Video', frame)
if cv2.waitKey(1) == 13: #13 Enter Key
break
cap.release() # camera release
cv2.destroyAllWindows()
import cv2
import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
img = cv2.imread('c:/Users/hari/Desktop/datasets/Naresh_datasets/car_plate.jpg')
def display(img):
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
new_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
ax.imshow(new_img)
display(img)
plate_cascade = cv2.CascadeClassifier('c:/Users/hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_russian_plate_number.xml')
def detect_plate(img):
plate_img = img.copy()
plate_rects = plate_cascade.detectMultiScale(plate_img,scaleFactor=1.3, minNeighbors=3)
for (x,y,w,h) in plate_rects:
cv2.rectangle(plate_img, (x,y), (x+w,y+h), (0,0,255), 4)
return plate_img
result = detect_plate(img)
display(result)
def detect_and_blur_plate(img):
plate_img = img.copy()
roi = img.copy()
plate_rects = plate_cascade.detectMultiScale(plate_img,scaleFactor=1.3, minNeighbors=3)
for (x,y,w,h) in plate_rects:
roi = roi[y:y+h,x:x+w]
blurred_roi = cv2.medianBlur(roi,7)
plate_img[y:y+h,x:x+w] = blurred_roi
return plate_img
result = detect_and_blur_plate(img)
display(result)
import numpy as np
import cv2
corner_track_params = dict(maxCorners = 10,
qualityLevel = 0.3,
minDistance = 7,
blockSize = 7 )
lk_params = dict( winSize = (200,200),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10,0.03))
cap = cv2.VideoCapture(0)
ret, prev_frame = cap.read()
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
prevPts = cv2.goodFeaturesToTrack(prev_gray, mask = None, **corner_track_params)
mask = np.zeros_like(prev_frame)
while True:
ret,frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
nextPts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, frame_gray, prevPts, None, **lk_params)
good_new = nextPts[status==1]
good_prev = prevPts[status==1]
for i,(new,prev) in enumerate(zip(good_new,good_prev)):
x_new,y_new = new.ravel()
x_prev,y_prev = prev.ravel()
mask = cv2.line(mask, (x_new,y_new),(x_prev,y_prev), (0,255,0), 3)
frame = cv2.circle(frame,(x_new,y_new),8,(0,0,255),-1)
img = cv2.add(frame,mask)
cv2.imshow('tracking',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
prev_gray = frame_gray.copy()
prevPts = good_new.reshape(-1,1,2)
cv2.destroyAllWindows()
cap.release()
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
prvsImg = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv_mask = np.zeros_like(frame1)
hsv_mask[:,:,1] = 255
while True:
ret, frame2 = cap.read()
nextImg = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prvsImg,nextImg, None, 0.5, 3, 15, 3, 5, 1.2, 0)
mag, ang = cv2.cartToPolar(flow[:,:,0], flow[:,:,1],angleInDegrees=True)
hsv_mask[:,:,0] = ang/2
hsv_mask[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv_mask,cv2.COLOR_HSV2BGR)
cv2.imshow('frame2',bgr)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
prvsImg = nextImg
cap.release()
cv2.destroyAllWindows()
import cv2
cv2.cartToPolar
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret,frame = cap.read()
print(frame)
face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml')
face_rects = face_cascade.detectMultiScale(frame)
print(face_rects)
(face_x,face_y,w,h) = tuple(face_rects[0])
track_window = (face_x,face_y,w,h)
roi = frame[face_y:face_y+h, face_x:face_x+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while True:
ret ,frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
ret, track_window = cv2.meanShift(dst, track_window, term_crit)
x,y,w,h = track_window
img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255),5)
cv2.imshow('img2',img2)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
else:
break
cv2.destroyAllWindows()
cap.release()
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret,frame = cap.read()
print(frame)# u can comment this
face_cascade = cv2.CascadeClassifier('c:/Users/Hari/Desktop/datasets/Naresh_datasets/haarcascades\\haarcascade_frontalface_default.xml')
face_rects = face_cascade.detectMultiScale(frame)
(face_x,face_y,w,h) = tuple(face_rects[0])
track_window = (face_x,face_y,w,h)
roi = frame[face_y:face_y+h, face_x:face_x+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while True:
ret ,frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
ret, track_window = cv2.CamShift(dst, track_window, term_crit)
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
img2 = cv2.polylines(frame,[pts],True, (0,0,255),5)
cv2.imshow('img2',img2)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
else:
break
cv2.destroyAllWindows()
cap.release()
# Other Tracking apis
import cv2
def ask_for_tracker():
print("What Tracker API would you like to use?")
print("Enter 0 for BOOSTING: ")
print("Enter 1 for MIL: ")
print("Enter 2 for KCF: ")
print("Enter 3 for TLD: ")
print("Enter 4 for MEDIANFLOW: ")
choice = input("Please select your tracker: ")
if choice == '0':
tracker = cv2.TrackerBoosting_create()
if choice == '1':
tracker = cv2.TrackerMIL_create()
if choice == '2':
tracker = cv2.TrackerKCF_create()
if choice == '3':
tracker = cv2.TrackerTLD_create()
if choice == '4':
tracker = cv2.TrackerMedianFlow_create()
return tracker
tracker = ask_for_tracker()
tracker_name = str(tracker).split()[0][1:]
print(tracker_name)
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
roi = cv2.selectROI(frame, False)
ret = tracker.init(frame, roi)
while True:
ret, frame = cap.read()
success, roi = tracker.update(frame)
(x,y,w,h) = tuple(map(int,roi))
if success:
# Tracking success
p1 = (x, y)
p2 = (x+w, y+h)
cv2.rectangle(frame, p1, p2, (0,255,0), 3)
else :
cv2.putText(frame, "Failure to Detect Tracking!!", (100,200), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),3)
cv2.putText(frame, tracker_name, (20,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),3);
cv2.imshow(tracker_name, frame)
k = cv2.waitKey(1) & 0xff
if k == 27 :
break
cap.release()
cv2.destroyAllWindows()
# Traffic Sign
import cv2
import numpy as np
from scipy.stats import itemfreq
def get_dominant_color(image, n_colors):
pixels = np.float32(image).reshape((-1, 3))
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
flags = cv2.KMEANS_RANDOM_CENTERS
flags, labels, centroids = cv2.kmeans(
pixels, n_colors, None, criteria, 10, flags)
palette = np.uint8(centroids)
print(palette[np.argmax(itemfreq(labels)[:, -1])])
return palette[np.argmax(itemfreq(labels)[:, -1])]
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.EVENT_LBUTTONUP:
clicked = True
cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('camera')
cv2.setMouseCallback('camera', onMouse)
success, frame = cameraCapture.read()
while success and not clicked:
cv2.waitKey(1)
success, frame = cameraCapture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray, 37)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT,
1, 50, param1=120, param2=40)
if not circles is None:
circles = np.uint16(np.around(circles))
max_r, max_i = 0, 0
for i in range(len(circles[:, :, 2][0])):
if circles[:, :, 2][0][i] > 50 and circles[:, :, 2][0][i] > max_r:
max_i = i
max_r = circles[:, :, 2][0][i]
x, y, r = circles[:, :, :][0][max_i]
if y > r and x > r:
square = frame[y-r:y+r, x-r:x+r]
dominant_color = get_dominant_color(square, 2)
if dominant_color[2] > 100:
print("STOP")
elif dominant_color[0] > 80:
zone_0 = square[square.shape[0]*3//8:square.shape[0]
* 5//8, square.shape[1]*1//8:square.shape[1]*3//8]
cv2.imshow('Zone0', zone_0)
zone_0_color = get_dominant_color(zone_0, 1)
zone_1 = square[square.shape[0]*1//8:square.shape[0]
* 3//8, square.shape[1]*3//8:square.shape[1]*5//8]
cv2.imshow('Zone1', zone_1)
zone_1_color = get_dominant_color(zone_1, 1)
zone_2 = square[square.shape[0]*3//8:square.shape[0]
* 5//8, square.shape[1]*5//8:square.shape[1]*7//8]
cv2.imshow('Zone2', zone_2)
zone_2_color = get_dominant_color(zone_2, 1)
if zone_1_color[2] < 60:
if sum(zone_0_color) > sum(zone_2_color):
print("LEFT")
else:
print("RIGHT")
else:
if sum(zone_1_color) > sum(zone_0_color) and sum(zone_1_color) > sum(zone_2_color):
print("FORWARD")
elif sum(zone_0_color) > sum(zone_2_color):
print("FORWARD AND LEFT")
else:
print("FORWARD AND RIGHT")
else:
print("N/A")
for i in circles[0, :]:
cv2.circle(frame, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv2.circle(frame, (i[0], i[1]), 2, (0, 0, 255), 3)
cv2.imshow('camera', frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27 :
break
cv2.destroyAllWindows()
cameraCapture.release()
# Car detection
import cv2
print(cv2.__version__)
import os
os.chdir('c:/Users/Hari/Desktop/datasets/Naresh_datasets/')
# cars.xml file will posses important role hare
cascade_src = 'cars.xml'
#load the video which has the cars moving
video_src = 'CarDetectionusingOpenCV.mp4'
cap = cv2.VideoCapture(video_src) # read the video
car_cascade = cv2.CascadeClassifier(cascade_src) # draw rectangls or mark or dectect the cars motions
# iterate loop
while True:
#tuple unpacking
ret, img = cap.read()
if(type(img) == type(None)):
break
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray,1.1,1)
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('video',img)
if cv2.waitKey(33) == 27:
break
cv2.destroyAllWindows()
import os
os.chdir('c:/Users/Hari/Desktop/datasets/Naresh_datasets/')
# cars.xml file will posses important role hare
cascade_src = 'cars.xml'
#load the video which has the cars moving
# video_src = 'CarDetectionusingOpenCV.mp4'
video_src1 = 'VehicleDetection.mp4'
cap = cv2.VideoCapture(video_src1) # read the video
car_cascade = cv2.CascadeClassifier(cascade_src) # draw rectangls or mark or dectect the cars motions
# iterate loop
while True:
#tuple unpacking
ret, img = cap.read()
if(type(img) == type(None)):
break
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray,1.1,1)
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('video',img)
if cv2.waitKey(33) == 27:
break
cv2.destroyAllWindows()
import cv2
from PIL import Image
e_img = cv2.imread('c:/Users/Hari/Desktop/datasets/reverse_img.jpg')
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(e_img) # BY default matplotlib load in BGR-color space model
img_l = Image.open('c:/Users/Hari/Desktop/datasets/reverse_img.jpg')
img_l
import cv2
img_f = cv2.flip(e_img,-1)
plt.imshow(img_f)
img_r = cv2.resize(img_f,(1200,1200))
plt.imshow(img_r)
plt.figure(figsize=(12,8))
ax = plt.subplot(111)
ax.imshow(img_r)
import numpy as np
img_b = np.zeros((1200,1200,3),np.uint8)
plt.imshow(img_b)
import cv2
cv2.imshow('imgre',img_r)
cv2.waitKey(0)
im= Image.open('c:/Users/Hari/Desktop/datasets/reverse_img.jpg')
im
from PIL import Image
im.transpose(Image.FLIP_TOP_BOTTOM)
from PIL import Image, ImageDraw
from math import floor
# Load image:
input_image = Image.open("input.png")
input_pixels = input_image.load()
new_size = (300, 300)
# Create output image
output_image = Image.new("RGB", new_size)
draw = ImageDraw.Draw(output_image)
x_scale = input_image.width / output_image.width
y_scale = input_image.height / output_image.height
# Copy pixels
for x in range(output_image.width):
for y in range(output_image.height):
xp, yp = floor(x * x_scale), floor(y * y_scale)
draw.point((x, y), input_pixels[xp, yp])
output_image.save("output.png")
import numpy as np
np.empty((3,4))